package com.wonders.cyxtjsonupdate;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wonders.cyxtjsonupdate.entity.Cyxt;
import com.wonders.cyxtjsonupdate.service.CyxtJsonData;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.*;

@SpringBootTest
class CyxtJsonUpdateApplicationTests1 {

    //-----------对外旅客发送量(人次) 、同比

    @Autowired
    CyxtJsonData cyxtJsonData;

    @Test
    void test1() {
        Calendar cal = Calendar.getInstance();
        NumberFormat nf = NumberFormat.getInstance();
        nf.setMaximumFractionDigits(0);//保留0位
        //获得当前月份
        int month = new Date().getMonth();
        month =11;//因目前1月份数据库未产生12月份数据，此此先模拟
        //获得当前年份
        int year = cal.get(Calendar.YEAR);
        ArrayList<String> list = new ArrayList<>();//用来存储所有的最终的json
        //初始对外旅客发送量的map
        Map<String, Object> travelerMap = cyxtJsonData.initTraveler().get(month - 1);
        //初始化公路旅客发送量的map
        Map<String, Object> highwayMap = cyxtJsonData.initHighway().get(month - 1);
        //初始化水路旅客发送量的map
        Map<String, Object> waterWayMap = cyxtJsonData.initWaterway().get(month - 1);
        //初始化铁路旅客发送量的map
        Map<String, Object> railwayMap = cyxtJsonData.initrailway().get(month - 1);
        //初始化航空旅客发送量的map
        Map<String, Object> aviationMap = cyxtJsonData.initaviation().get(month - 1);

        //初始化公路旅客发送量的list
        List<Map<String, Object>> highwayList = cyxtJsonData.initHighway();
        //初始化水路旅客发送量的list
        List<Map<String, Object>> waterWayList = cyxtJsonData.initWaterway();
        //初始化铁路旅客发送量的list
        List<Map<String, Object>> railWayList = cyxtJsonData.initrailway();
        //初始化航空旅客发送量的list
        List<Map<String, Object>> aviationList = cyxtJsonData.initaviation();
        //初始化拼接月份的集合
        List monthList = buildMonth(month);


        //对外旅客月均
        Cyxt cyxt1= buildgeneralpParame("fc3120c3bd234608a0413fc73718d737", "kyLazy", "ydwlkfsl");
        buildPassengerTransportMonthAvg(cyxt1,month,travelerMap,list,nf);
        //对外旅客同比增量
        Cyxt cyxt2 = buildgeneralpParame("44181093a0324ed095afa669ea385a79", "kyLazy", "ydwlkfslbfb");
        buildTravelerYeartoYeartotal(cyxt2,travelerMap,list);
        //对外旅客累计
        Cyxt cyxt3 = buildgeneralpParame("9ab8c165b7e14d2cab1f63e0e98da18d", "kyLazy", "tdwlkfsl");
        buildTravelerYeartotal(cyxt3,travelerMap,list,nf);
        //对外旅客同比增长
        Cyxt cyxt4 = buildgeneralpParame("161fa26ff8784d93818a4a2127d8c9d1", "kyLazy", "tdwlkfslbfb");
        buildTravelerYeartoYeartotal(cyxt4,travelerMap,list);


        //公路旅客月均
        Cyxt cyxt5 = buildgeneralpParame("9c071c5aaa47459ba649fbd912ff5dff", "kyLazy", "ygllkfsl");
        buildPassengerTransportMonthAvg(cyxt5,month,highwayMap,list,nf);
        //公路旅客同比增量
        Cyxt cyxt6 = buildgeneralpParame("2e222537b72142f79050175e64590644", "kyLazy", "ygllkfslbfb");
        buildTravelerYeartoYeartotal(cyxt6,highwayMap,list);
        //公路旅客累计
        Cyxt cyxt7 = buildgeneralpParame("d153d635324e4ff89887651110e7c85b", "kyLazy", "tgllkfsl");
        buildTravelerYeartotal(cyxt7,highwayMap,list,nf);
        //公路旅客同比增长
        Cyxt cyxt8 = buildgeneralpParame("d02237ec3809405cbc8bacc5fd33dfb3", "kyLazy", "tgllkfslbfb");
        buildTravelerYeartoYeartotal(cyxt8,highwayMap,list);

        //水路旅客月均
        Cyxt cyxt9= buildgeneralpParame("200df9601ead4f67a25433cfe1c5abaa", "kyLazy", "ysllkfsl");
        buildPassengerTransportMonthAvg(cyxt9,month,waterWayMap,list,nf);
        //水路旅客同比增量
        Cyxt cyxt10 = buildgeneralpParame("61cdfbb4b13f44f3b5b35e08ce33d7cf", "kyLazy", "ysllkfslbfb");
        buildTravelerYeartoYeartotal(cyxt10,waterWayMap,list);
        //水路旅客累计
        Cyxt cyxt11 = buildgeneralpParame("0541819cca33439197a3024ee5429791", "kyLazy", "tsllkfsl");
        buildTravelerYeartotal(cyxt11,waterWayMap,list,nf);
        //水路旅客同比增长
        Cyxt cyxt12 = buildgeneralpParame("0bebc290650e4df583fed17b484b7b59", "kyLazy", "tsllkfslbfb");
        buildTravelerYeartoYeartotal(cyxt12,waterWayMap,list);

        //铁路旅客月均
        Cyxt cyxt13 = buildgeneralpParame("d4287df230c44c02ae876cbd65758ba0", "kyLazy", "ytllvfsl");
        buildPassengerTransportMonthAvg(cyxt13,month,railwayMap,list,nf);
        //铁路旅客同比增量
        Cyxt cyxt14 = buildgeneralpParame("26720b6e34e5416a9eec756f7c6f0862", "kyLazy", "ytllvfslbfb");
        buildTravelerYeartoYeartotal(cyxt14,railwayMap,list);
        //铁路旅客累计
        Cyxt cyxt15 = buildgeneralpParame("460d2764670d43f68256cd849842ca10", "kyLazy", "ttllvfsl");
        buildTravelerYeartotal(cyxt15,railwayMap,list,nf);
        //铁路旅客同比增长
        Cyxt cyxt16 = buildgeneralpParame("fa396d34aa2347f085e12dd48930bfc3", "kyLazy", "ttllvfslbfb");
        buildTravelerYeartoYeartotal(cyxt16,railwayMap,list);

        //航空旅客月均
        Cyxt cyxt17 = buildgeneralpParame("a3ebd93786eb4b26b89d6471111c5abe", "kyLazy", "yhklvfsl");
        buildPassengerTransportMonthAvg(cyxt17,month,aviationMap,list,nf);
        //航空旅客同比增量
        Cyxt cyxt18 = buildgeneralpParame("70c2d442e7cd4518808cf7e4797ff967", "kyLazy", "yhklvfslbfb");
        buildTravelerYeartoYeartotal(cyxt18,aviationMap,list);
        //航空旅客累计
        Cyxt cyxt19 = buildgeneralpParame("45cae5c9c559416ebdd21c2a52d2f4ce", "kyLazy", "thklvfsl");
        buildTravelerYeartotal(cyxt19,aviationMap,list,nf);
        //航空旅客同比增长
        Cyxt cyxt20 = buildgeneralpParame("f94e25b8f16b4c288ebe1ddaa28f142f", "kyLazy", "thklvfslbfb");
        buildTravelerYeartoYeartotal(cyxt20,aviationMap,list);





        //------------本年、去年【四个方式】日均旅客发送量
        Cyxt cyxt22 = buildgeneralpParame("ef508270cc4e45e0a0ec680f9e839840", "kyLazy", "myChart1");
        cyxt22.setDataItem1("[{text: '公路', max: 150000},{text: '水路', max: 150000},{text: '铁路', max: 150000},{text: '航空', max: 150000}]");
        ArrayList<Integer> allYearAvg = new ArrayList<>();
        //获得截止需统计月份的天数
        int days = getDays(year, month);
        //公路本年日均
        BigDecimal road1 = new BigDecimal("0");
        BigDecimal highWayYearSum = new BigDecimal(highwayList.get(month-1).get("YEARSUM").toString());
        road1 = highWayYearSum.multiply(new BigDecimal("10000")).divide(new BigDecimal(days + ""), 0, BigDecimal.ROUND_HALF_UP);
        allYearAvg.add(road1.intValue());
        //水路本年日均
        BigDecimal waterWay = new BigDecimal("0");
        BigDecimal waterWayYearSum = new BigDecimal(waterWayList.get(month-1).get("YEARSUM").toString());
        waterWay = waterWayYearSum.multiply(new BigDecimal("10000")).divide(new BigDecimal(days + ""), 0, BigDecimal.ROUND_HALF_UP);
        allYearAvg.add(waterWay.intValue());

        //铁路本年日均
        BigDecimal railWay = new BigDecimal("0");
            BigDecimal railWaySum = new BigDecimal(railWayList.get(month-1).get("YEARSUM").toString());
        railWay = railWaySum.multiply(new BigDecimal("10000")).divide(new BigDecimal(days + ""), 0, BigDecimal.ROUND_HALF_UP);
        allYearAvg.add(railWay.intValue());
        //机场本年日均
        BigDecimal airport = new BigDecimal("0");
        BigDecimal airportYearSum = new BigDecimal(aviationList.get(month-1).get("YEARSUM").toString());
        airport = airportYearSum.multiply(new BigDecimal("10000")).divide(new BigDecimal(days + ""), 0, BigDecimal.ROUND_HALF_UP);
        allYearAvg.add(airport.intValue());

        cyxt22.setDataItem2(allYearAvg.toString());
        String tbzz8str = JSONObject.toJSONString(cyxt22, SerializerFeature.WriteMapNullValue);
        list.add(tbzz8str);

        //获得去年对应月份天数
        int lastDays = getlastYearDays(year, month);

        //去年【四个方式】日均旅客发送量

        Cyxt cyxt23 = buildgeneralpParame("5e87bd0fab814f0596b1bcfe79038000","kyLazy","myChart2");
        cyxt23.setDataItem1("[{text: '公路', max: 350000},{text: '水路', max: 350000},{text: '铁路', max: 350000},{text: '航空', max: 350000}]");
        ArrayList<Integer> lastYearAvg = new ArrayList<>();
        //公路去年日均
        BigDecimal road2 = new BigDecimal("0");
        BigDecimal highWayLastYearSum = new BigDecimal(highwayList.get(month-1).get("SNTQLJ").toString());
        road2 = highWayLastYearSum.multiply(new BigDecimal("10000")).divide(new BigDecimal(lastDays + ""), 0, BigDecimal.ROUND_HALF_UP);
        lastYearAvg.add(road2.intValue());
        //水路去年日均
        BigDecimal waterroad = new BigDecimal("0");
        BigDecimal waterWayLastYearSum = new BigDecimal(waterWayList.get(month-1).get("SNTQLJ").toString());
        waterroad = waterWayLastYearSum.multiply(new BigDecimal("10000")).divide(new BigDecimal(lastDays + ""), 0, BigDecimal.ROUND_HALF_UP);
        lastYearAvg.add(waterroad.intValue());
        //铁路去年日均
        BigDecimal railwayrood = new BigDecimal("0");
        BigDecimal railWayLastYearSum = new BigDecimal(railWayList.get(month-1).get("SNTQLJ").toString());
        railwayrood = railWayLastYearSum.multiply(new BigDecimal("10000")).divide(new BigDecimal(lastDays + ""), 0, BigDecimal.ROUND_HALF_UP);
        lastYearAvg.add(railwayrood.intValue());
        //航空去年日均
        BigDecimal air = new BigDecimal("0");
        BigDecimal aviationLastYearSum = new BigDecimal(aviationList.get(month-1).get("SNTQLJ").toString());
        air = aviationLastYearSum.multiply(new BigDecimal("10000")).divide(new BigDecimal(lastDays + ""), 0, BigDecimal.ROUND_HALF_UP);
        lastYearAvg.add(air.intValue());
        cyxt23.setDataItem2(lastYearAvg.toString());
        String tbzz9str = JSONObject.toJSONString(cyxt23, SerializerFeature.WriteMapNullValue);
        list.add(tbzz9str);
        //-------------旅客发送量月分布
        Cyxt cyxt24 = buildgeneralpParame("64219af6d8274f0cbcf9d2de5812d72e", "kyLazy", "myChart3");
        buildTravelerMonthdata(cyxt24,month,aviationList,waterWayList,highwayList,railWayList,list);
        //-------------四条折线图
        //--公路
        buildChart("a34520ab73c54dbab77dd984202ee45d","kyLazy","myChart4",month,"MONTHSUM", "SNTY",monthList,highwayList,list);
        //--水路
        buildChart("94e700453c7a4c508a32b431ae4c834b","kyLazy","myChart5",month,"MONTHSUM", "SNTY",monthList,waterWayList,list);
        //--铁路
        buildChart("3a9ba7d98c3d44df8340f70291b3574d","kyLazy","myChart6",month,"MONTHSUM", "SNTY",monthList,railWayList,list);
        //--航空
        buildChart("82bfec42ede446f5a64bb4acbf018556","kyLazy","myChart7",month,"MONTHSUM", "SNTY",monthList,aviationList,list);



        System.out.println(list);
    }

    /**
     * Methad info:----月均
     * @author 陈兆庆
     * @date 2022/1/5 16:02
     */
     public void buildPassengerTransportMonthAvg(Cyxt cyxt, int month, Map<String, Object> dataMap, List list, NumberFormat nf) {
         //月均
         Object yearsum = dataMap.get("YEARSUM");
         double yearsumd = Double.parseDouble(yearsum.toString());
         double monthavg = yearsumd / month * 10000;
         String format = nf.format(monthavg);
         cyxt.setDataItem1(format);
         String yjhy = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
         list.add(yjhy);
     }

    /**
     * Methad info:---同比增长
     * @author 陈兆庆
     * @date 2022/1/5 14:15
     */

    public void buildTravelerYeartoYeartotal(Cyxt cyxt, Map<String, Object> dataMap, List list) {
        //-- 同比增量（BNBSNTQZJB字段）
        Object tbzz = dataMap.get("BNBSNTQZJB");
        cyxt.setDataItem1(tbzz + "%");
        String tbzz2str = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(tbzz2str);
    }

    /**
     * Methad info:------累计
     * @author 陈兆庆
     * @date 2022/1/5 14:12
     */

    public void buildTravelerYeartotal(Cyxt cyxt, Map<String, Object> dataMap, List list, NumberFormat nf) {
        //--累计（yearSum）
        Object yearsum =dataMap.get("YEARSUM");
        double yearsum1 = Double.parseDouble(yearsum.toString()) * 10000;
        String format1 = nf.format(yearsum1);
        cyxt.setDataItem1(format1);
        String lj = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(lj);
    }

    /**
     * Methad info:-------------旅客发送量月分布
     * @author 流光亦断
     * @date 2022/1/5 18:10
     */

    public void buildTravelerMonthdata(Cyxt cyxt,int month,List<Map<String, Object>> aviationList,List<Map<String, Object>> waterWayList,List<Map<String, Object>> highwayList,List<Map<String, Object>> railWayList,List list){
        ArrayList<List<Integer>> dataList = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < month; j++) {
                if (i == 0) {
                    ArrayList<Integer> allfreightList = new ArrayList<>();
                    allfreightList.add(0);
                    allfreightList.add(j);
                    Object monthsum = aviationList.get(j).get("MONTHSUM");
                    BigDecimal multiply = new BigDecimal(monthsum.toString()).multiply(new BigDecimal("10000"));
                    allfreightList.add(multiply.intValue());
                    dataList.add(allfreightList);
                } else if (i == 1) {
                    ArrayList<Integer> allfreightList = new ArrayList<>();
                    allfreightList.add(1);
                    allfreightList.add(j);
                    Object monthsum = waterWayList.get(j).get("MONTHSUM");
                    BigDecimal multiply = new BigDecimal(monthsum.toString()).multiply(new BigDecimal("10000"));
                    allfreightList.add(multiply.intValue());
                    dataList.add(allfreightList);
                } else if (i == 2) {
                    ArrayList<Integer> allfreightList = new ArrayList<>();
                    allfreightList.add(2);
                    allfreightList.add(j);

                    Object monthsum = highwayList.get(j).get("MONTHSUM");
                    BigDecimal multiply = new BigDecimal(monthsum.toString()).multiply(new BigDecimal("10000"));
                    allfreightList.add(multiply.intValue());
                    dataList.add(allfreightList);
                } else if (i == 3) {
                    ArrayList<Integer> allfreightList = new ArrayList<>();
                    allfreightList.add(3);
                    allfreightList.add(j);
                    Object monthsum = railWayList.get(j).get("MONTHSUM");
                    BigDecimal multiply = new BigDecimal(monthsum.toString()).multiply(new BigDecimal("10000"));
                    allfreightList.add(multiply.intValue());
                    dataList.add(allfreightList);
                }
            }
        }
        cyxt.setDataItem1(dataList.toString());
        cyxt.setDataItem2("['机场', '水路', '公路', '铁路']");
        cyxt.setDataItem3(buildMonth(month).toString().replace(" ",""));
        String tbzzstr = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(tbzzstr);
    }


    /**
     * Methad info:四个折线图(月趋势)
     * @author 陈兆庆
     * @date 2022/1/4 13:36
     */
    public void buildChart(String id, String mainType, String subType, int month, String keys, String snty, List monthList, List<Map<String, Object>> listdata, List list) {
        Cyxt cyxt = new Cyxt();
        cyxt.setId(id);
        cyxt.setMainType(mainType);
        cyxt.setSubType(subType);
        cyxt.setDataItem1(monthList.toString().replace(" ",""));
        ArrayList<Integer> roadChart1 = new ArrayList<>();
        for (int i = 0; i < month; i++) {
            Object monthsum = listdata.get(i).get(keys);
            /*     Integer ceil = (int)Math.ceil(Double.valueOf(monthsum.toString()))*10000;//此处数据有差异，后续确认是否向上取整*/
            double ceila = Double.parseDouble(monthsum.toString());
            int ceil = (int) (ceila * 10000);
            roadChart1.add(ceil);
        }
        //去年月总
        ArrayList<Integer> roadChart2 = new ArrayList<>();
        for (int i = 0; i < month; i++) {
            Object monthsum = listdata.get(i).get(snty);
            /*       Integer ceil = (int)Math.ceil(Double.valueOf(monthsum.toString()))*10000;//此处数据有差异，后续确认是否向上取整*/
            double ceila = Double.parseDouble(monthsum.toString());
            int ceil = (int) (ceila * 10000);
            roadChart2.add(ceil);
        }
        cyxt.setDataItem2(roadChart1.toString().replace(" ",""));
        cyxt.setDataItem3(roadChart2.toString().replace(" ",""));
        String tbzz10str = JSONObject.toJSONString(cyxt, SerializerFeature.WriteMapNullValue);
        list.add(tbzz10str);
    }




    /**
     * Methad info:计算今年月份截止天数
     * @author 陈兆庆
     * @date 2022/1/5 17:22
     */
   public int getDays(int year,int month) {
       int days=0;
       for (int i = 0; i < month; i++) {
           if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11) {
               days+=31;
           } else if (i == 3 || i == 5 || i == 8 || i == 10) {
               days+=30;
           } else {
               if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
                   days+=29;
               } else {
                   days+=29;
               }
           }
       }
       return days;
   }

    /**
     * Methad info:计算上年月份截止天数
     * @author 陈兆庆
     * @date 2022/1/5 17:22
     */
    public int getlastYearDays(int year,int month) {
        int days=0;
        for (int i = 0; i < month; i++) {
            if (i == 0 || i == 2 || i == 4 || i == 6 || i == 7 || i == 9 || i == 11) {
                days+=31;
            } else if (i == 3 || i == 5 || i == 8 || i == 10) {
                days+=30;
            } else {
                if ((year-1) % 4 == 0 && (year-1) % 100 != 0 || (year-1) % 400 == 0) {
                    days+=29;
                } else {
                    days+=29;
                }
            }
        }
        return days;
    }















    /**
     * Methad info:通用的参数
     * @return com.wonders.cyxtjsonupdate.entity.Cyxt
     * @author 陈兆庆
     * @date 2022/1/4 14:47
     */
    public Cyxt buildgeneralpParame(String id, String mainType, String subType) {
        Cyxt cyxt = new Cyxt();
        cyxt.setId(id);
        cyxt.setMainType(mainType);
        cyxt.setSubType(subType);
        return cyxt;
    }

    /**
     * Methad info:拼接月份
     * @author 陈兆庆
     * @date 2022/1/4 16:14
     */
    public List buildMonth(int month) {
        ArrayList<String> monthList = new ArrayList<>();
        //拼接月份
        for (int i = 0; i < month; i++) {
            if (i == 0) {
                monthList.add("'1月'");
            } else if (i == 1) {
                monthList.add("'2月'");
            } else if (i == 2) {
                monthList.add("'3月'");
            } else if (i == 3) {
                monthList.add("'4月'");
            } else if (i == 4) {
                monthList.add("'5月'");
            } else if (i == 5) {
                monthList.add("'6月'");
            } else if (i == 6) {
                monthList.add("'7月'");
            } else if (i == 7) {
                monthList.add("'8月'");
            } else if (i == 8) {
                monthList.add("'9月'");
            } else if (i == 9) {
                monthList.add("'10月'");
            } else if (i == 10) {
                monthList.add("'11月'");
            } else if (i == 11) {
                monthList.add("'12月'");
            }
        }
        return monthList;
    }

}
